package com.admin.education.controllers.business;

import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.jwt.JwtAuthorize;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.subject.Request.CreateSubjectRequest;
import com.admin.education.dataTransferObject.subject.Request.QuerySubjectRequest;
import com.admin.education.dataTransferObject.subject.Request.UpdateSubjectRequest;
import com.admin.education.dataTransferObject.subject.SubjectTransfer;
import com.admin.education.services.Interfaces.ISubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.Callable;

@JwtAuthorize
@RestController
@RequestMapping("subjects")
@CrossOrigin(origins = "*", maxAge = 3600)
public class SubjectController {
    @Autowired
    private ISubjectService subjectService;
    @GetMapping("")
    public Callable<IResponseMessage> querySubjects(@Valid QuerySubjectRequest pageRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult<PageResponse<SubjectTransfer>> servicesExcuteResult = subjectService.querySubjects(pageRequest);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessageWithData<PageResponse<SubjectTransfer>>(Result.Success,servicesExcuteResult.getData());
                }
                else{
                    throw new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return  responseMessage;
            }
        };
    }

    @PostMapping("")
    public Callable<IResponseMessage> createSubject(@RequestBody @Valid CreateSubjectRequest createSubjectRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult servicesExcuteResult = subjectService.createSubject(createSubjectRequest);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @PutMapping("{subjectId}")
    public Callable<IResponseMessage> updateSubject(@PathVariable Long subjectId,@RequestBody @Valid UpdateSubjectRequest updateSubjectRequest){
        return  new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult servicesExcuteResult = subjectService.updateSubject(subjectId,updateSubjectRequest);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @DeleteMapping("{subjectId}")
    public Callable<IResponseMessage> validCategory(@PathVariable Long  subjectId)
    {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult servicesExcuteResult = subjectService.deleteSubject(subjectId);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @GetMapping("{id}")
    public Callable<IResponseMessage> querySubjectById(@PathVariable Long id)
    {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult<SubjectTransfer> servicesExcuteResult = subjectService.querySubjectById(id);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessageWithData<SubjectTransfer>(Result.Success,servicesExcuteResult.getData());
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
}
